-- best_player
-- Created by chengb Oct/22/2015
-- 最佳玩家排行榜子模块

-- 内部函数声明
local trySubmit;
local whenDungeonScoreRefresh;
local whenGainPet;
local whenPetUpgradeRank;

-- 注册迷宫得分刷新的事件
EventMgr.register("gamecenter_leaderboard/best_player", event.DUNGEON_SCORE_REFRESH, function(args)
    whenDungeonScoreRefresh(args);
end);

-- 注册获得冈布奥的事件
EventMgr.register("gamecenter_leaderboard/best_player", event.GAIN_PET, function(args)
    whenGainPet(args);
end);

-- 注册获得冈布奥的事件
EventMgr.register("gamecenter_leaderboard/best_player", event.UPGRADE_PET_RANK, function(args)
    whenPetUpgradeRank(args);
end);

-- 迷宫得分刷新的事件回调
function whenDungeonScoreRefresh(args)
    trySubmit();
end

-- 获得冈布奥的事件回调
function whenGainPet(args)
    trySubmit();
end

-- 冈布奥升级的事件回调
function whenPetUpgradeRank(args)
    trySubmit();
end

-- 尝试提交分数
function trySubmit()
    -- 计算总分
    local score = FormulaM.invoke("CALC_PLAYER_COMPREHENSIVE_SCORE", ME.user);

    local ids = GameCenterLeaderboardM.getIdsByRule("best_player");
    for _, id in pairs(ids) do
        -- 尝试提交分数
        GameCenterLeaderboardM.submitScore(id, score);
    end
end


